package sliver.action.shop.ajax;

import java.io.Writer;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import sliver.service.shop.ads.AdvertisementService;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller("shop.ajax.recordPrintTwoDimensionalCodeAction")
public class RecordPrintTwoDimensionalCodeAction extends ActionSupport{

	private static final long serialVersionUID = -952934096013855308L;
	
	private Integer shopperId;
	private Long machineId;
	private Long advertisementId;
	
	@Autowired
	private AdvertisementService advertisementService;
	
	public RecordPrintTwoDimensionalCodeAction(){
		
	}

	public Integer getShopperId() {
		return shopperId;
	}

	public void setShopperId(Integer shopperId) {
		this.shopperId = shopperId;
	}

	public Long getMachineId() {
		return machineId;
	}

	public void setMachineId(Long machineId) {
		this.machineId = machineId;
	}

	public Long getAdvertisementId() {
		return advertisementId;
	}

	public void setAdvertisementId(Long advertisementId) {
		this.advertisementId = advertisementId;
	}
	
	@Override
	public String execute() throws Exception {
		
		advertisementService.printTwoDimensionalCode(shopperId, machineId, advertisementId);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/plain;charset=utf-8");
		
		Writer writer = response.getWriter();
		writer.write(JSON.toJSONString("printed!"));
		writer.flush();
		
		return null;
	}
}